﻿package com.thorqq.magictimer;

import java.util.ArrayList;
import java.util.List;

import com.thorqq.magictimer.R;
import com.thorqq.magictimer.util.Util;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public abstract class FragmentActivityBase extends FragmentActivity
{

    private ViewPager      mViewPager;
    private PagerTabStrip  mPagerTabStrip;
    protected ViewGroup    mLayoutBottom;
    
    private Button mBtnSave;
    private Button mBtnCancel;
    
    private List<Fragment> mFragmentList = new ArrayList<Fragment>();
    private List<String>   mTitleList    = new ArrayList<String>();
    private LinearLayout   mLayoutIndicator;
    private ImageView[]    mIvIndicatorArray; 
    

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        initLayout();
    }
    
    protected void setBottonLayoutVisibility(int visibility)
    {
        mLayoutBottom.setVisibility(visibility);
    }
    
    public void setFragmentList(List<Fragment> fragmentList, List<String> titleList)
    {
        mFragmentList = fragmentList;
        mTitleList = titleList;
        
        updateLayout();
        regListener();
    }
    
    protected abstract void doOnClickSave();
    protected abstract void doOnClickCancel();
    
    private void regListener()
    {
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            
            @Override
            public void onPageSelected(int arg0)
            {
                mIvIndicatorArray[arg0].setBackgroundResource(R.drawable.viewpager_indicator_on);
                for (int i = 0; i < mIvIndicatorArray.length; i++) 
                {  
                    if (arg0 != i) 
                    {  
                        mIvIndicatorArray[i].setBackgroundResource(R.drawable.viewpager_indicator_off);  
                    }  
                }
            }
            
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2)
            {                
            }
            
            @Override
            public void onPageScrollStateChanged(int arg0)
            {                
            }
        });

        mBtnSave.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            {
                doOnClickSave();
            }
        });

        mBtnCancel.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            {
                doOnClickCancel();
            }
        });

    }

    private void initLayout()
    {
        setContentView(R.layout.fragment_base);

        mViewPager = (ViewPager)findViewById(R.id.ViewPager);
        mPagerTabStrip = (PagerTabStrip)findViewById(R.id.PagerTabStrip);
        mLayoutIndicator = (LinearLayout)findViewById(R.id.layoutIndicator);
        mLayoutBottom = (ViewGroup)findViewById(R.id.layoutBottom);

        mBtnSave = (Button) findViewById(R.id.btnSave);
        mBtnCancel = (Button) findViewById(R.id.btnCancel);
    }

    private void updateLayout()
    {
        Util.log("FragmentActivityBase.updateLayout");
        
        //mPagerTabStrip.setBackgroundResource(R.drawable.btn_new_policy_normal);
        mPagerTabStrip.setBackgroundColor(0x50000000);
        //mPagerTabStrip.setDrawFullUnderline(true);
        mPagerTabStrip.setPadding(5, 12, 5, 10);
        mPagerTabStrip.setTabIndicatorColor(0x70aaaaaa);

//        mViewPager.setAdapter(mPagerAdapter);        
        mViewPager.setAdapter(new myPagerAdapter(getSupportFragmentManager(), mFragmentList, mTitleList));        

        //指示器
        mIvIndicatorArray = new ImageView[mFragmentList.size()];  
        for (int i = 0; i < mFragmentList.size(); i++) 
        {  
            mIvIndicatorArray[i] = new ImageView(this);  
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(8,8);
            
            if (i == 0) 
            {  
                lp.leftMargin = 0;
                mIvIndicatorArray[i].setLayoutParams(lp);  
                mIvIndicatorArray[i].setBackgroundResource(R.drawable.viewpager_indicator_on);  
            } else 
            {  
                lp.leftMargin = 8;
                mIvIndicatorArray[i].setLayoutParams(lp);  
                mIvIndicatorArray[i].setBackgroundResource(R.drawable.viewpager_indicator_off);  
            }  
            
            mLayoutIndicator.addView(mIvIndicatorArray[i]);  
        }  
    }

    /**
     * 定义适配器
     * 
     * @author gxwu@lewatek.com 2012-11-15
     */
    class myPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragmentList;
        private List<String>   titleList;

        public myPagerAdapter(FragmentManager fm, List<Fragment> fragmentList, List<String> titleList){
            super(fm);
            this.fragmentList = fragmentList;
            this.titleList = titleList;
        }

        @Override
        public Fragment getItem(int arg0) {
            return (fragmentList == null || fragmentList.size() == 0) ? null : fragmentList.get(arg0);
        }

        public CharSequence getPageTitle(int position) {
            return (titleList.size() > position) ? titleList.get(position) : "";
        }

        @Override
        public int getCount() {
            return fragmentList == null ? 0 : fragmentList.size();
        }

    }

}
